package com.share.lottery.adapter;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.TextView;

import com.share.lottery.R;
import com.share.lottery.model.AddItem;

/**
 * @author py
 *多选
 */
public class SelectAdapter extends BaseAdapter {
	private List<AddItem> list = new ArrayList<AddItem>();
	private Context context;
	private List<AddItem> selects = new ArrayList<AddItem>();

	public List<AddItem> getSelects() {
		return selects;
	}
	public void setSelects(List<AddItem> selects) {
		this.selects = selects;
	}
	public SelectAdapter(Context context) {
		this.context = context;
	}
	public int getCount() {
		return list.size();
	}


	public AddItem getItem(int arg0) {
		return list.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		return arg0;
	}


	public void setSelct(int postion,boolean b){
		AddItem myiItem = list.get(postion);
		myiItem.setB(b);
		if (myiItem.isB()) {
			selects.add(myiItem);
		}else {
			selects.remove(myiItem);
		}
		notifyDataSetChanged();
	}

	public View getView(int position, View view, ViewGroup arg2) {
		ViewHolder holder = null;
		if (view == null) {
			holder = new ViewHolder();
			view = View.inflate(context, R.layout.listviewitem, null);
			holder = new ViewHolder();
			holder.tv = (TextView) view.findViewById(R.id.item_tv);
			holder.cb = (CheckBox) view.findViewById(R.id.item_cb);
		
			view.setTag(holder);
		} else {
			holder = (ViewHolder) view.getTag();
		}
		AddItem item = getItem(position);
		holder.tv.setText(item.getName());
		holder.cb.setChecked(item.isB());


		return view;
	}
	private class ViewHolder {
		public TextView tv = null;
		public CheckBox cb = null;

	}

	public void addData(List<AddItem> items){
		list.addAll(items);
		notifyDataSetChanged();
	}
	public void clearData(){
		list.clear();
		notifyDataSetChanged();
	}

}



